<?php
class NOTES{
    //general variables
	var $dbErrorMsg = "Failed set/get data from database (Class notes): ";

    /**
     * function for getting the logs
     * return : array with all records in note
     */
    function getNotes($relTable,$relId,$orderby='modif', $order='desc'){
    	$tmpUser = USERS::getUser(-1);
        $res = MAIN::returnArray("select `".tblPrefix."notes`.*, `".tblPrefix."users`.email,CONCAT(`".tblPrefix."persons`.`firstname`,' ',`".tblPrefix."persons`.`lastname`) as 'fullname'
                                  from `".tblPrefix."notes`
                                  inner join `".tblPrefix."users`
                                  on `".tblPrefix."notes`.`user_id` = `".tblPrefix."users`.`id`
                                  LEFT join `".tblPrefix."persons`
                                  on `".tblPrefix."users`.id = `".tblPrefix."persons`.`user_id`
                                  WHERE `description`='".$relTable."' 
                                  AND `field1`=".$relId."
                                  AND (`".tblPrefix."notes`.`user_id`=".MAIN::decode($_SESSION[svar])." OR `".tblPrefix."users`.`grouplevel`<".$tmpUser['grouplevel'].")
                                  order by ".$orderby." ".$order);
        return $res;
    }
    /**
     * Function for saving entry into the note
     *
     */
    function setNote($type, $desc, $str1, $str2){
        if(isset($_SESSION[svar])){
            $userId = MAIN::decode($_SESSION[svar]);
        }else{
            $userId = -1;
        }
        $sql = "insert into `".tblPrefix."notes` (`type`, `description`, `field1`, `field2`, `user_id`) values ('".$type."', '".$desc."', '".$str1."', '".$str2."', ".$userId.")";
        return MAIN::executeCommand($sql);
    }
	
	function getUsernotes($userId,$orderby='modif', $order='desc'){
        $res = MAIN::returnArray("select `".tblPrefix."notes`.*, `".tblPrefix."users`.email
                                  from `".tblPrefix."notes`
                                  inner join `".tblPrefix."users`
                                  on `".tblPrefix."notes`.user_id = `".tblPrefix."users`.id
                                  where `".tblPrefix."notes`.field1=".$userId."
                                  order by ".$orderby." ".$order);
        return $res;
    }
    function delNote($noteId){
    	$sql="delete from `".tblPrefix."notes` where `id`=".$noteId;
    	MAIN::executeCommand($sql);
    }

}
?>